﻿Public Class CreateBackground
    Private TLC As Bitmap = My.Resources._01
    Private TRC As Bitmap = My.Resources._02
    Private BLC As Bitmap = My.Resources._03
    Private BRC As Bitmap = My.Resources._04
    Private ColMat As Single()() = {New Single() {100, 0, 0, 0, 0}, New Single() {0, 100, 0, 0, 0}, New Single() {0, 0, 100, 0, 0}, New Single() {0, 0, 0, 100, 0}, New Single() {0, 0, 0, 0, 100}}
    Private BasePic As Bitmap
    Private OutPic As Bitmap
    Private M_OP As Integer = 100
    Private M_Flip As Boolean = False
    Private M_Width As Integer
    Private M_Height As Integer
    Private M_FaceColor As Color
    Public Function CreatePic(ByVal Width As Integer, ByVal Height As Integer) As Bitmap
        M_Width = Width
        M_Height = Height
        DrawBase()
        Colorize(Color.Silver)
        BasePic = New Bitmap(OutPic)
        Return BasePic
    End Function
    Public Function CreatePic(ByVal Width As Integer, ByVal Height As Integer, ByVal Color As Color) As Bitmap
        M_Width = Width
        M_Height = Height
        DrawBase()
        Colorize(Color)
        BasePic = New Bitmap(OutPic)
        M_FaceColor = BasePic.GetPixel(10, 10)
        Return BasePic
    End Function
    Public Function CreateScaledPic(ByVal Width As Integer, ByVal Height As Integer, ByVal Color As Color, ByVal Scale As Double) As Bitmap
        M_Width = Width / Scale
        M_Height = Height / Scale
        DrawBase()
        Colorize(Color)
        BasePic = New Bitmap(Width, Height, Imaging.PixelFormat.Format32bppPArgb)
        Dim G As Graphics = Graphics.FromImage(BasePic)
        G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        G.DrawImage(OutPic, 0, 0, Width, Height)
        M_FaceColor = BasePic.GetPixel(10, 10)
        Return BasePic
    End Function
    Private Sub DrawBase()
        Dim BP As New Bitmap(M_Width, M_Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        TLC.MakeTransparent(Color.White)
        TRC.MakeTransparent(Color.White)
        BLC.MakeTransparent(Color.White)
        BRC.MakeTransparent(Color.White)
        Dim g As Graphics = Graphics.FromImage(BP)
        Dim X As Integer
        Dim Y As Integer
        Dim W As Integer = TLC.Width / 3
        Dim H As Integer = TLC.Height / 3
        Dim r As New Rectangle(W / 2, H / 2, M_Width - W, M_Height - H)
        Dim Col As Color = BRC.GetPixel(0, 0)
        Dim BR As New SolidBrush(Col)
        g.Clear(Color.Transparent)
        g.FillRectangle(BR, r)
        For X = W To (BP.Width - W)
            g.DrawImage(TRC, X, 0, W, H)
            g.DrawImage(BRC, X, BP.Height - H, W, H)
        Next X
        For Y = H To (BP.Height - H)
            g.DrawImage(BLC, 0, Y, W, H)
            g.DrawImage(BRC, BP.Width - W, Y, W, H)
        Next
        g.DrawImage(BLC, 0, BP.Height - H, W, H)
        g.DrawImage(TLC, 0, 0, W, H)
        BR.Dispose()
        BasePic = New Bitmap(BP)
        BP.Dispose()
    End Sub
    Public ReadOnly Property FaceColor() As Color
        Get
            Return M_FaceColor
        End Get
    End Property
    Private Sub Colorize(ByVal Col As Color)
        OutPic = New Bitmap(BasePic.Width, BasePic.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        Dim R As Double
        Dim G As Double
        Dim B As Double
        Dim OP As Double
        Dim S As Double = 100 / 255
        R = (Col.R * S) / 100
        G = (Col.G * S) / 100
        B = (Col.B * S) / 100
        OP = M_OP / 100
        Dim Mat As Single()() = { _
                New Single() {R, 0, 0, 0, 0}, _
                New Single() {0, G, 0, 0, 0}, _
                New Single() {0, 0, B, 0, 0}, _
                New Single() {0, 0, 0, OP, 0}, _
                New Single() {0, 0, 0, 0, 1}}

        Dim colorMatrix As New System.Drawing.Imaging.ColorMatrix(Mat)
        ' Create an ImageAttributes object and set its color matrix.
        Dim imageAtt As New System.Drawing.Imaging.ImageAttributes()
        imageAtt.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap)
        imageAtt.SetColorKey(System.Drawing.Color.Black, System.Drawing.Color.White)
        Dim r1 As RectangleF
        r1.X = 0
        r1.Y = 0
        r1.Width = OutPic.Width
        r1.Height = OutPic.Height
        Dim r2 As RectangleF
        r2.X = 0
        r2.Y = 0
        r2.Width = OutPic.Width
        r2.Height = OutPic.Height
        Dim Grap As Graphics = Graphics.FromImage(OutPic)
        Dim brush3 As New TextureBrush(BasePic, r1, imageAtt)
        Grap.FillRectangle(brush3, r2)
        Grap.Dispose()
        brush3.Dispose()

    End Sub

End Class
